﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using NCT.Entity;
using System.Threading;
using Microsoft.Phone;
using NCT.DataAccessComponents;

namespace NCT.MusicApplication
{
    public partial class PlaySongPage : PhoneApplicationPage
    {

        public PlaySongPage()
        {
            InitializeComponent();
            SongPlaying = ((FrameworkElement)Application.Current.RootVisual).DataContext as Item;
        }

        private void mediaPlayer_BufferingProgressChanged(object sender, RoutedEventArgs e)
        {
            MediaElement me = (MediaElement)sender;
            if (me.BufferingProgress == 1)
            {
                
            }
            
        }
        public double Duration { get; set; }
        DependencyProperty property = DependencyProperty.Register("Progress", typeof(double), typeof(PhoneApplicationPage), new PropertyMetadata(0.0));
        public double Progress
        {
            get
            {
                return (double)GetValue(property);
            }
            set
            {
                SetValue(property, value);
            }
        }

        public string SongTitle
        {
            get
            {
                return (this.DataContext as Item).Name + " - " + (this.DataContext as Item).Singer;
            }
        }
        public string SongLiked
        {
            get
            {
                return (this.DataContext as Item).Liked;
            }
        }
        public string SongListen
        {
            get
            {
                return (this.DataContext as Item).Listen;
            }
        }
        public string SongPoster
        {
            get
            {
                return (this.DataContext as Item).Poster;
            }
        }

        private void mediaPlayer_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (mediaPlayer.CurrentState == MediaElementState.Playing)
            {
                Duration = mediaPlayer.NaturalDuration.TimeSpan.TotalSeconds;
                ThreadPool.QueueUserWorkItem(o =>
                {
                    while (true)
                    {
                        Dispatcher.BeginInvoke(new Action(() => {
                                //Progress = mediaPlayer.Position.TotalSeconds * 100 / Duration;
                            update();    
                        }));
                            
                            Thread.Sleep(0);
                    }
                });
            }
            if (mediaPlayer.CurrentState == MediaElementState.Stopped)
            {
                mediaPlayer.Play();
            }

        }
        void update()
        {
            Progress = mediaPlayer.Position.TotalSeconds * 100 / Duration;
            songTime.Text = String.Format("{0:d2}:{1:d2}", mediaPlayer.NaturalDuration.TimeSpan.Minutes, mediaPlayer.NaturalDuration.TimeSpan.Seconds);
            currentTime.Text = String.Format("{0:d2}:{1:d2}", mediaPlayer.Position.Minutes, mediaPlayer.Position.Seconds); 
        }
        ItemDAC id = new ItemDAC();
        public Item SongPlaying
        {
            get
            {
                return this.DataContext as Item;
            }
            set
            {
                this.DataContext = value;
                mediaPlayer.Source = new Uri((DataContext as Item).Url, UriKind.Absolute);
                mediaPlayer.Stop();
                mediaPlayer.Play();

                //Lấy thông tin Bài hát hiện thời
                id.Item = value;
                id.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(id_PropertyChanged);
                ItemsPreparation itemDAC = (this.Resources["songItemsPresenter"] as ItemsPreparation);
                itemDAC.getSearch(1, 10, value.Name, "searchSong");
               
            }
        }

        void id_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "LyricSong")
            {
                songText.Text = id.LyricSong;
            } if (e.PropertyName == "CommentSong")
            {
                // 
            }
        }

        private void itemControl_Navigation(object sender, NavigationListControl.NavigationEventArgs e)
        {
            SongPlaying = e.Item as Item;
        }

      

   }
}